﻿using System;
using System.Collections.Generic;

namespace OF.Utility
{
    /// <summary>
    ///  分页列表接口类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface IPageOfList<T>
    {
        /// <summary>
        /// 集合记录总数
        /// </summary>
        Int32 ItemCount { get; }
        /// <summary>
        /// 页数
        /// </summary>
        Int32 PageCount { get; }
        /// <summary>
        /// 当前页码
        /// </summary>
        Int32 PageIndex { get; }
        /// <summary>
        /// 页面大小，即每页需要显示的记录个数
        /// </summary>
        Int32 PageSize { get; }
    }
    /// <summary>
    /// 分页列表 实现类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PageOfList<T> : List<T>, IPageOfList<T>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="items">T的一个可枚举集合</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="itemCount">记录总数</param>
        public PageOfList(IEnumerable<T> items, int pageIndex, int pageSize, int itemCount)
        {
            AddRange(items);
            PageIndex = pageIndex;
            PageSize = pageSize;
            ItemCount = itemCount;
            PageCount = (int)Math.Ceiling(itemCount / (double)pageSize);
        }

        #region IPageOfList<T> 成员
        /// <summary>
        /// 集合记录总数
        /// </summary>
        public int ItemCount { get; private set; }

        /// <summary>
        /// 页数
        /// </summary>
        public int PageCount { get; private set; }

        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageIndex { get; private set; }

        /// <summary>
        /// 页面大小，即每页需要显示的记录个数
        /// </summary>
        public int PageSize { get; private set; }

        #endregion

    }
}
